<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8" />
    <title>21_案例练习-移入一级菜单显示对应的二级菜单-静态页面搭建</title>

    <style>
        * {
            margin: 0;
            padding: 0;
        }

        a {
            text-decoration: none;
            color: black;
        }

        .list {
            position: relative;
            margin-left: 20px;
            width: 200px;
            height: 150px;
            border: 1px solid black;
        }

        li {
            list-style: none;
        }

        .list>li {
            width: 200px;
            height: 50px;
            text-align: center;
            line-height: 50px;
            background-color: aqua;
        }

        .list>li:nth-child(2) {
            border-top: 1px solid #000;
            border-bottom: 1px solid #000;
            box-sizing: border-box;
        }

        .listIn {
            position: absolute;
            left: 200px;
            top: -1px;
            display: none;

            width: 200px;
            height: 150px;
            border: 1px solid black;
        }

        .listIn li {
            width: 200px;
            height: 50px;
            background-color: hotpink;
        }

        .listIn li:nth-child(2) {
            border-top: 1px solid #000;
            border-bottom: 1px solid #000;
            box-sizing: border-box;
        }
    </style>
</head>

<body>
    <ul class="list">
        <li>
            <a href="#">中国</a>
            <ul class="listIn">
                <li><a href="#">金城武</a></li>
                <li><a href="#">黄宗泽</a></li>
                <li><a href="#">宋小宝</a></li>
            </ul>
        </li>
        <li>
            <a href="#">韩国</a>
            <ul class="listIn">
                <li><a href="#">朴灿烈</a></li>
                <li><a href="#">马东锡</a></li>
                <li><a href="#">张根硕</a></li>
            </ul>
        </li>
        <li>
            <a href="#">日本</a>
            <ul class="listIn">
                <li><a href="#">美一利亚</a></li>
                <li><a href="#">小栗旬</a></li>
                <li><a href="#">樱木花道</a></li>
            </ul>
        </li>
    </ul>
    <script>
        var li_list = document.querySelectorAll('.list > li');
        for (var i = 0; i < li_list.length; i++) {
            // 给每个一级菜单li绑定事件
            li_list[i].index = i;
            li_list[i].onmouseover = function () {
                //给每个对应的二级菜单设置属性为隐藏
                for(var j = 0; j < li_list.length; j++){
                    var list_in = li_list[j].querySelector('.listIn');
                    list_in.style.display = 'none';
                }
                

                //给移入li的二级菜单设置为显示
                li_list[this.index].querySelector('.listIn').style.display = 'block';
            }
        }

    </script>
</body>

</html>